package com.toobigtofail.utility
{
	import com.toobigtofail.entities.Enemy;
	import com.toobigtofail.entities.BuildingBlock;
	import com.toobigtofail.entities.Target;
	
	import Box2D.Dynamics.Contacts.b2Contact;
	import Box2D.Dynamics.b2ContactListener;
	
	import fabulaexmachina.box2fp.Box2DEntity;
	
	public class ContactListener extends b2ContactListener
	{
		public function ContactListener()
		{
			super();
		}
		
		/**
		 * Called when two fixtures begin to touch.
		 */
		override public function BeginContact(contact:b2Contact):void {
			var typeA:String = contact.GetFixtureA().GetBody().GetUserData().type;
			var typeB:String = contact.GetFixtureB().GetBody().GetUserData().type;
			
			if (typeA == Target.TYPE && (typeB == BuildingBlock.TYPE || typeB == Enemy.TYPE)) {
				(contact.GetFixtureA().GetBody().GetUserData() as Box2DEntity).remove(); 
			}
			else if (typeB == Target.TYPE && (typeA == BuildingBlock.TYPE || typeA == Enemy.TYPE)){
				(contact.GetFixtureB().GetBody().GetUserData() as Box2DEntity).remove(); 
			}
		}
	}
}